package com.sg.service.biz.bff.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.bff.req.BffQueryIconicAchievementsDetailsListReqDto;
import com.sg.dto.biz.bff.req.BffQueryNewDockAchievementListReqDto;
import com.sg.dto.biz.bff.res.BffQueryIconicAchievementsDetailsListRespDto;
import com.sg.dto.biz.bff.res.BffQueryNewDockAchievementListRespDto;
import com.sg.service.biz.bff.DashboardAchievementService;
import com.wicket.okrapp.integration.AchievementService;
import com.wicket.okrapp.integration.DashboardAchievementService;
import com.wicket.okrapp.integration.TargetCalcService;
import com.wicket.okrapp.integration.dto.*;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class DashboardAchievementServiceImpl
        implements DashboardAchievementService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    com.sg.service.biz.app.AchievementService fwAppAchievementClient;
    @Resource
    com.sg.service.biz.app.TargetCalcService fwAppTargetCalcClient;

    @Resource
    com.sg.service.biz.app.DashboardAchievementService fwAppDashboardAchievementClient;

    /**
     * B3查询最新成果列表[9345]
     * gen by moon at 6/30/2024, 10:22:08 PM
     */
    @Trace(operationName = "B3查询最新成果列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryNewDockAchievementListRespDto queryNewDockAchievementList(BffQueryNewDockAchievementListReqDto reqDto) {


        QueryAchievementDockingListComRespDto queryAchievementDockingListComRespDto_1 = null;
        BatchQueryAchievementInfoComRespDto batchQueryAchievementInfoComRespDto_1 = null;
//步骤0: D3查询对接成果关系列表(公共) - queryAchievementDockingListCom
        QueryAchievementDockingListComRespDto queryAchievementDockingListComRespDto = null;
        QueryAchievementDockingListComReqDto queryAchievementDockingListComReqDto = new QueryAchievementDockingListComReqDto();
        queryAchievementDockingListComReqDto.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:1709440_1
        queryAchievementDockingListComReqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:1709441_1
        if (reqDto != null) {
            queryAchievementDockingListComReqDto.setEvaluationTemplateId(reqDto.getEvaluationTemplateId());//SimpleFieldAssign//sourceId:1709439_1
        }

        /*D3查询对接成果关系列表(公共)[9146]   */
        Assert.isNull(queryAchievementDockingListComReqDto.getEvaluationTemplateId(), "B3查询最新成果列表-D3查询对接成果关系列表(公共)-归属评价模板ID不能为空", false);
        Assert.isNull(queryAchievementDockingListComReqDto.getSubjectLifeCycle(), "B3查询最新成果列表-D3查询对接成果关系列表(公共)-主体生命周期不能为空", false);
        Assert.isNull(queryAchievementDockingListComReqDto.getSpaceId(), "B3查询最新成果列表-D3查询对接成果关系列表(公共)-创建于空间ID不能为空", false);
        queryAchievementDockingListComRespDto = fwAppAchievementClient.queryAchievementDockingListCom(queryAchievementDockingListComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        queryAchievementDockingListComRespDto_1 = queryAchievementDockingListComRespDto;

//步骤1: D3批量查询成果信息(公共) - batchQueryAchievementInfoCom
        BatchQueryAchievementInfoComRespDto batchQueryAchievementInfoComRespDto = null;
        if (queryAchievementDockingListComRespDto != null) {
            BatchQueryAchievementInfoComReqDto batchQueryAchievementInfoComReqDto = new BatchQueryAchievementInfoComReqDto();
            batchQueryAchievementInfoComReqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:1709445_1
            if (queryAchievementDockingListComRespDto != null && queryAchievementDockingListComRespDto.getAchievementDockingList() != null && !CollectionUtil.isEmpty(queryAchievementDockingListComRespDto.getAchievementDockingList())) {
                batchQueryAchievementInfoComReqDto.setAchievementList(queryAchievementDockingListComRespDto.getAchievementDockingList().stream().map(item -> item.getAchievementId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:1709444_1
            }

            /*D3批量查询成果信息(公共)[9317]   */
            Assert.isNull(batchQueryAchievementInfoComReqDto.getSpaceId(), "B3查询最新成果列表-D3批量查询成果信息(公共)-创建于空间ID不能为空", false);
            batchQueryAchievementInfoComRespDto = fwAppAchievementClient.batchQueryAchievementInfoCom(batchQueryAchievementInfoComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


            batchQueryAchievementInfoComRespDto_1 = batchQueryAchievementInfoComRespDto;
        }

        BffQueryNewDockAchievementListRespDto retData = new BffQueryNewDockAchievementListRespDto();
        //数据集融合  MoreListToOneSource
        if (queryAchievementDockingListComRespDto_1 != null && !CollectionUtil.isEmpty(queryAchievementDockingListComRespDto_1.getAchievementDockingList())) {
            for (AchievementDockingDto achievementDockingDto : queryAchievementDockingListComRespDto_1.getAchievementDockingList()) {
                BffAchievementDockingDto retElm = new BffAchievementDockingDto();
                if (queryAchievementDockingListComRespDto_1 != null) {
                    retElm.setAchievementId(achievementDockingDto.getAchievementId());//SimpleFieldAssign//sourceId:360774_2
                    retElm.setDutyDeptName(achievementDockingDto.getDutyDeptName());//SimpleFieldAssign//sourceId:386703_2
                }
                retData.getAchievementDockingList().add(retElm);
                if (batchQueryAchievementInfoComRespDto_1 != null) {
                    for (AchievementDto achievementDto : batchQueryAchievementInfoComRespDto_1.getAchievementList()) {
                        if (achievementDto.getAchievementId().equals(achievementDockingDto.getAchievementId())) {
                            if (batchQueryAchievementInfoComRespDto_1 != null) {
                                retElm.setAchievementName(achievementDto.getAchievementName());//SimpleFieldAssign//sourceId:360775_2
                                retElm.setAwardedTime(achievementDto.getAwardedTime());//SimpleFieldAssign//sourceId:360776_2
                                retElm.setValidEndTime(achievementDto.getValidEndTime());//SimpleFieldAssign//sourceId:360777_2
                                retElm.setBelongName(achievementDto.getBelongName());//SimpleFieldAssign//sourceId:360778_2
                                retElm.setIsSymbolicAchievements(achievementDto.getIsSymbolicAchievements());//SimpleFieldAssign//sourceId:360779_2
                                retElm.setAchievementTypeId(achievementDto.getAchievementTypeId());//SimpleFieldAssign//sourceId:360780_2
                                retElm.setAchievementTypeNumber(achievementDto.getAchievementTypeNumber());//SimpleFieldAssign//sourceId:360781_2
                                retElm.setAchievementTypeName(achievementDto.getAchievementTypeName());//SimpleFieldAssign//sourceId:386357_2
                                retElm.setAchievementCategoryId(achievementDto.getAchievementCategoryId());//SimpleFieldAssign//sourceId:360783_2
                                retElm.setAchievementCategoryNumber(achievementDto.getAchievementCategoryNumber());//SimpleFieldAssign//sourceId:360784_2
                                retElm.setAchievementCategoryName(achievementDto.getAchievementCategoryName());//SimpleFieldAssign//sourceId:360785_2
                                retElm.setAchievementGradeId(achievementDto.getAchievementGradeId());//SimpleFieldAssign//sourceId:378974_2
                                retElm.setAchievementGradeNumber(achievementDto.getAchievementGradeNumber());//SimpleFieldAssign//sourceId:378975_2
                                retElm.setAchievementGradeName(achievementDto.getAchievementGradeName());//SimpleFieldAssign//sourceId:378976_2
                                retElm.setAchievementBusinessGradeId(achievementDto.getAchievementBusinessGradeId());//SimpleFieldAssign//sourceId:378977_2
                                retElm.setAchievementBusinessGradeNumber(achievementDto.getAchievementBusinessGradeNumber());//SimpleFieldAssign//sourceId:378978_2
                                retElm.setAchievementBusinessGradeName(achievementDto.getAchievementBusinessGradeName());//SimpleFieldAssign//sourceId:378979_2
                                retElm.setCopyrightObjectName(achievementDto.getCopyrightObjectName());//SimpleFieldAssign//sourceId:386695_2
                            }
                        }
                    }
                }

            }
        }//sourceId:1709447_1


        return retData;
    }

    /**
     * B3查询标志性成果明细列表[9928]
     * gen by moon at 7/4/2024, 1:00:30 AM
     */
    @Trace(operationName = "B3查询标志性成果明细列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryIconicAchievementsDetailsListRespDto queryIconicAchievementsDetailsList(BffQueryIconicAchievementsDetailsListReqDto reqDto) {


        QueryIconicAchievementsDetailsListComRespDto queryIconicAchievementsDetailsListComRespDto_1 = null;
        BatchQueryAchievementInfoComRespDto batchQueryAchievementInfoComRespDto_1 = null;
//步骤0: D3查询标志性成果明细列表(公共) - queryIconicAchievementsDetailsListCom
        QueryIconicAchievementsDetailsListComRespDto queryIconicAchievementsDetailsListComRespDto = null;
        QueryIconicAchievementsDetailsListComReqDto queryIconicAchievementsDetailsListComReqDto = new QueryIconicAchievementsDetailsListComReqDto();
        if (reqDto != null) {
            queryIconicAchievementsDetailsListComReqDto.setDashboradCoreId(reqDto.getDashboradCoreId());//SimpleFieldAssign//sourceId:1919545_1
            queryIconicAchievementsDetailsListComReqDto.setDashboradCoreTypeCode(reqDto.getDashboradCoreTypeCode());//SimpleFieldAssign//sourceId:1919546_1
            queryIconicAchievementsDetailsListComReqDto.setParentSubMidCycleType(reqDto.getParentSubMidCycleType());//SimpleFieldAssign//sourceId:1918302_1
            queryIconicAchievementsDetailsListComReqDto.setAchievementTypeId(reqDto.getAchievementTypeId());//SimpleFieldAssign//sourceId:1918314_1
            queryIconicAchievementsDetailsListComReqDto.setAchievementGradeId(reqDto.getAchievementGradeId());//SimpleFieldAssign//sourceId:1918311_1
            queryIconicAchievementsDetailsListComReqDto.setEvaluationTemplateId(reqDto.getEvaluationTemplateId());//SimpleFieldAssign//sourceId:1918312_1
        }

        /*D3查询标志性成果明细列表(公共)[9927]   */
        Assert.isNull(queryIconicAchievementsDetailsListComReqDto.getDashboradCoreId(), "B3查询标志性成果明细列表-D3查询标志性成果明细列表(公共)-大屏主体ID不能为空", false);
        Assert.isNull(queryIconicAchievementsDetailsListComReqDto.getDashboradCoreTypeCode(), "B3查询标志性成果明细列表-D3查询标志性成果明细列表(公共)-大屏主体类型编码不能为空", false);
        Assert.isNull(queryIconicAchievementsDetailsListComReqDto.getParentSubMidCycleType(), "B3查询标志性成果明细列表-D3查询标志性成果明细列表(公共)-父子中周期类型不能为空", false);
        Assert.isNull(queryIconicAchievementsDetailsListComReqDto.getEvaluationTemplateId(), "B3查询标志性成果明细列表-D3查询标志性成果明细列表(公共)-冗余评价模板ID不能为空", false);
        queryIconicAchievementsDetailsListComRespDto = fwAppDashboardAchievementClient.queryIconicAchievementsDetailsListCom(queryIconicAchievementsDetailsListComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        queryIconicAchievementsDetailsListComRespDto_1 = queryIconicAchievementsDetailsListComRespDto;

//步骤1: D3批量查询成果信息(公共) - batchQueryAchievementInfoCom
        BatchQueryAchievementInfoComRespDto batchQueryAchievementInfoComRespDto = null;
        if (queryIconicAchievementsDetailsListComRespDto != null) {
            BatchQueryAchievementInfoComReqDto batchQueryAchievementInfoComReqDto = new BatchQueryAchievementInfoComReqDto();
            batchQueryAchievementInfoComReqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:1918319_1
            if (queryIconicAchievementsDetailsListComRespDto != null && queryIconicAchievementsDetailsListComRespDto.getAchievementDockingList() != null && !CollectionUtil.isEmpty(queryIconicAchievementsDetailsListComRespDto.getAchievementDockingList())) {
                batchQueryAchievementInfoComReqDto.setAchievementList(queryIconicAchievementsDetailsListComRespDto.getAchievementDockingList().stream().map(item -> item.getAchievementId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:1918318_1
            }

            /*D3批量查询成果信息(公共)[9317]   */
            Assert.isNull(batchQueryAchievementInfoComReqDto.getSpaceId(), "B3查询标志性成果明细列表-D3批量查询成果信息(公共)-创建于空间ID不能为空", false);
            batchQueryAchievementInfoComRespDto = fwAppAchievementClient.batchQueryAchievementInfoCom(batchQueryAchievementInfoComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


            batchQueryAchievementInfoComRespDto_1 = batchQueryAchievementInfoComRespDto;
        }

        BffQueryIconicAchievementsDetailsListRespDto retData = new BffQueryIconicAchievementsDetailsListRespDto();
        //数据集融合  MoreListToOneSource
        if (queryIconicAchievementsDetailsListComRespDto_1 != null && !CollectionUtil.isEmpty(queryIconicAchievementsDetailsListComRespDto_1.getAchievementDockingList())) {
            for (AchievementDockingDto achievementDockingDto : queryIconicAchievementsDetailsListComRespDto_1.getAchievementDockingList()) {
                BffAchievementDockingDto retElm = new BffAchievementDockingDto();
                if (queryIconicAchievementsDetailsListComRespDto_1 != null) {
                    retElm.setAchievementId(achievementDockingDto.getAchievementId());//SimpleFieldAssign//sourceId:388286_2
                }
                retData.getAchievementDockingList().add(retElm);
                if (batchQueryAchievementInfoComRespDto_1 != null) {
                    for (AchievementDto achievementDto : batchQueryAchievementInfoComRespDto_1.getAchievementList()) {
                        if (achievementDto.getAchievementId().equals(achievementDockingDto.getAchievementId())) {
                            if (batchQueryAchievementInfoComRespDto_1 != null) {
                                retElm.setAchievementName(achievementDto.getAchievementName());//SimpleFieldAssign//sourceId:388291_2
                                retElm.setAwardedTime(achievementDto.getAwardedTime());//SimpleFieldAssign//sourceId:388292_2
                                retElm.setBelongName(achievementDto.getBelongName());//SimpleFieldAssign//sourceId:388293_2
                                retElm.setIsSymbolicAchievements(achievementDto.getIsSymbolicAchievements());//SimpleFieldAssign//sourceId:388294_2
                                retElm.setAchievementTypeId(achievementDto.getAchievementTypeId());//SimpleFieldAssign//sourceId:388295_2
                                retElm.setAchievementTypeNumber(achievementDto.getAchievementTypeNumber());//SimpleFieldAssign//sourceId:388296_2
                                retElm.setAchievementTypeName(achievementDto.getAchievementTypeName());//SimpleFieldAssign//sourceId:388297_2
                                retElm.setAchievementCategoryId(achievementDto.getAchievementCategoryId());//SimpleFieldAssign//sourceId:388298_2
                                retElm.setAchievementCategoryNumber(achievementDto.getAchievementCategoryNumber());//SimpleFieldAssign//sourceId:388299_2
                                retElm.setAchievementCategoryName(achievementDto.getAchievementCategoryName());//SimpleFieldAssign//sourceId:388300_2
                                retElm.setAchievementGradeId(achievementDto.getAchievementGradeId());//SimpleFieldAssign//sourceId:388301_2
                                retElm.setAchievementGradeNumber(achievementDto.getAchievementGradeNumber());//SimpleFieldAssign//sourceId:388302_2
                                retElm.setAchievementGradeName(achievementDto.getAchievementGradeName());//SimpleFieldAssign//sourceId:388303_2
                                retElm.setAchievementBusinessGradeId(achievementDto.getAchievementBusinessGradeId());//SimpleFieldAssign//sourceId:388304_2
                                retElm.setAchievementBusinessGradeNumber(achievementDto.getAchievementBusinessGradeNumber());//SimpleFieldAssign//sourceId:388305_2
                                retElm.setAchievementBusinessGradeName(achievementDto.getAchievementBusinessGradeName());//SimpleFieldAssign//sourceId:388306_2
                                retElm.setCopyrightObjectName(achievementDto.getCopyrightObjectName());//SimpleFieldAssign//sourceId:388307_2
                            }
                        }
                    }
                }

            }
        }//sourceId:1918367_1


        retData.setPageNum(queryIconicAchievementsDetailsListComRespDto.getPageNum());
        retData.setPageSize(queryIconicAchievementsDetailsListComRespDto.getPageSize());
        retData.setTotal(queryIconicAchievementsDetailsListComRespDto.getTotalNum());
        retData.setIsLastPage((Math.ceil(Double.valueOf(queryIconicAchievementsDetailsListComRespDto.getTotalNum()) / Double.valueOf(queryIconicAchievementsDetailsListComRespDto.getPageSize()))) <= retData.getPageNum());


        return retData;
    }
    //
}
